
package cn.edu.scau.cmi.hombio.dao;

import cn.edu.scau.cmi.hombio.domain.Pig;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Pig entities.
 * 
 */
@Repository("PigDAO")

@Transactional
public class PigDAOImpl extends AbstractJpaDao<Pig> implements PigDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Pig.class }));

	/**
	 * EntityManager injected by Spring for persistence unit pig
	 *
	 */
	@PersistenceContext(unitName = "pig")
	private EntityManager entityManager;

	/**
	 * Instantiates a new PigDAOImpl
	 *
	 */
	public PigDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findAllPigs
	 *
	 */
	@Transactional
	public Set<Pig> findAllPigs() throws DataAccessException {

		return findAllPigs(-1, -1);
	}

	/**
	 * JPQL Query - findAllPigs
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pig> findAllPigs(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllPigs", startResult, maxRows);
		return new LinkedHashSet<Pig>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigByState
	 *
	 */
	@Transactional
	public Set<Pig> findPigByState(Boolean state) throws DataAccessException {

		return findPigByState(state, -1, -1);
	}

	/**
	 * JPQL Query - findPigByState
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pig> findPigByState(Boolean state, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigByState", startResult, maxRows, state);
		return new LinkedHashSet<Pig>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigByRfidContaining
	 *
	 */
	@Transactional
	public Set<Pig> findPigByRfidContaining(String rfid) throws DataAccessException {

		return findPigByRfidContaining(rfid, -1, -1);
	}

	/**
	 * JPQL Query - findPigByRfidContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pig> findPigByRfidContaining(String rfid, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigByRfidContaining", startResult, maxRows, rfid);
		return new LinkedHashSet<Pig>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigByNumber
	 *
	 */
	@Transactional
	public Set<Pig> findPigByNumber(String number) throws DataAccessException {

		return findPigByNumber(number, -1, -1);
	}

	/**
	 * JPQL Query - findPigByNumber
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pig> findPigByNumber(String number, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigByNumber", startResult, maxRows, number);
		return new LinkedHashSet<Pig>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigByEarNumber
	 *
	 */
	@Transactional
	public Set<Pig> findPigByEarNumber(Integer earNumber) throws DataAccessException {

		return findPigByEarNumber(earNumber, -1, -1);
	}

	/**
	 * JPQL Query - findPigByEarNumber
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pig> findPigByEarNumber(Integer earNumber, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigByEarNumber", startResult, maxRows, earNumber);
		return new LinkedHashSet<Pig>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigByRfid
	 *
	 */
	@Transactional
	public Set<Pig> findPigByRfid(String rfid) throws DataAccessException {

		return findPigByRfid(rfid, -1, -1);
	}

	/**
	 * JPQL Query - findPigByRfid
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pig> findPigByRfid(String rfid, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigByRfid", startResult, maxRows, rfid);
		return new LinkedHashSet<Pig>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigByBirthday
	 *
	 */
	@Transactional
	public Set<Pig> findPigByBirthday(java.util.Calendar birthday) throws DataAccessException {

		return findPigByBirthday(birthday, -1, -1);
	}

	/**
	 * JPQL Query - findPigByBirthday
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pig> findPigByBirthday(java.util.Calendar birthday, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigByBirthday", startResult, maxRows, birthday);
		return new LinkedHashSet<Pig>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigByMemoContaining
	 *
	 */
	@Transactional
	public Set<Pig> findPigByMemoContaining(String memo) throws DataAccessException {

		return findPigByMemoContaining(memo, -1, -1);
	}

	/**
	 * JPQL Query - findPigByMemoContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pig> findPigByMemoContaining(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigByMemoContaining", startResult, maxRows, memo);
		return new LinkedHashSet<Pig>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigByMemo
	 *
	 */
	@Transactional
	public Set<Pig> findPigByMemo(String memo) throws DataAccessException {

		return findPigByMemo(memo, -1, -1);
	}

	/**
	 * JPQL Query - findPigByMemo
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pig> findPigByMemo(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigByMemo", startResult, maxRows, memo);
		return new LinkedHashSet<Pig>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigByNumberContaining
	 *
	 */
	@Transactional
	public Set<Pig> findPigByNumberContaining(String number) throws DataAccessException {

		return findPigByNumberContaining(number, -1, -1);
	}

	/**
	 * JPQL Query - findPigByNumberContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pig> findPigByNumberContaining(String number, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigByNumberContaining", startResult, maxRows, number);
		return new LinkedHashSet<Pig>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigById
	 *
	 */
	@Transactional
	public Pig findPigById(Integer id) throws DataAccessException {

		return findPigById(id, -1, -1);
	}

	/**
	 * JPQL Query - findPigById
	 *
	 */

	@Transactional
	public Pig findPigById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPigById", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.Pig) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findPigByPrimaryKey
	 *
	 */
	@Transactional
	public Pig findPigByPrimaryKey(Integer id) throws DataAccessException {

		return findPigByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findPigByPrimaryKey
	 *
	 */

	@Transactional
	public Pig findPigByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPigByPrimaryKey", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.Pig) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Pig entity) {
		return true;
	}
}
